package com.android.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.R;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.model.LoginListener;
import com.android.model.LoginModel;
import com.android.model.LoginModelImpl;

public class LoginActivity extends AppCompatActivity implements LoginListener, View.OnClickListener {
    private static final String TAG ="LoginActivity";

    private ResponseData responseData;
    private EditText etPwd;
    private EditText etAccount;
    private Button LoginButton;

    private String account;
    private String password;
    private LoginModel loginModel = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginModel = new LoginModelImpl();

        etPwd = findViewById(R.id.et_pwd);
        etAccount = findViewById(R.id.et_acc);

        LoginButton = findViewById(R.id.bt_login);

        LoginButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        account = etAccount.getText().toString();
        password = etPwd.getText().toString();
        loginModel.longin(account,password,this);

    }

    @Override
    public void onSuccess(ResponseData<User> responseData) {

        this.responseData = responseData;
        login();

    }

    private void login() {
        if(responseData.getCode() == 200){
            Intent intent = new Intent(LoginActivity.this,HomePageActivity.class);
            startActivity(intent);
        }else {
            Toast.makeText(LoginActivity.this,"账号或密码错误",Toast.LENGTH_SHORT);
        }
    }


    @Override
    public void onError() {

    }
}